<?php

// Configuration

define("IMAGE_JPEG_QUALITY", 90);
define("THUMB_LONG_EDGE", 160);

// End Configuration

// folder view (albums and images)

// image view (allows scrolling through images)

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>David Martin - Rambling Man</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="/css/home.css" media="all" />
</head>  
<body><div id="galleryMain">';

// get the album path
$albumPath = '.';
if (isset($_GET["albumPath"]))
{
	$albumPath = $_GET["albumPath"];
}


// some sort of header...

if (isset($_GET["imagePath"]))
{
	echo getImageBreadcrumb($_GET["imagePath"]);
	echo getImageMenu($_GET["imagePath"]);
	echo '<div id="contentBox">';
	echoImageView($_GET["imagePath"]);
	echo '</div>';
}
else
{
	echo getAlbumBreadcrumb($albumPath);
	echo '<div id="contentBox">';
	echoAlbumView($albumPath);
	echo '</div>';
}

echo '<div id="footer">
		<p>
			<span class="copyright">Copyright &copy; 2011 David Martin (<a href="http://www.ramblingman.co.nz">www.ramblingman.co.nz</a>)</span> 
		</p> 
	</div>
';

echo "</div></body></html>";

// FUNCTIONS ONLY FROM HERE ON

function echoImageView($imagePath)
{
	// write the image and its title
	echo '<div class="galleryImage"><img class="galleryImage" src="' . $imagePath . '"/><p id="galleryImageTitle">' . getImageTitle($imagePath) . '</p></div>';
}

function echoAlbumView($albumPath)
{
	// write albums
	foreach (albumList($albumPath) as $album)
	{
		echo '<div class="thumbBox"><div class="thumb"><a class="thumb" href="index2.php?albumPath='.$album.'"><img class="thumb" src="'.getAlbumThumbnailPath($album).'"/><p>'.getAlbumTitle($album).'</p></a></div></div>';
	}
	
	// write image thumbnails
	foreach (imageList($albumPath) as $image)
	{
		echo '<div class="thumbBox"><div class="thumb"><a class="thumb" href="index2.php?imagePath='.$image.'"><img class="thumb" src="'.getThumbnailPath($image).'"/></a></div></div>';
	}
}

function getAlbumThumbnailPath($albumPath)
{
	foreach (imageList($albumPath) as $image)
	{
		// need to select the thumbnail somehow, rather than just use the first...
		return getThumbnailPath($image);
	}
}

function getThumbnailPath($imagePath)
{
	// create the thumbnails path
	$thumbPath = '_thumbs/' . $imagePath;

	// create the thumbnail if the file does not exist
	if (!file_exists($thumbPath))
	{
		// get just the directory for the thumbnails
		$structure = pathinfo($thumbPath);
		$structure = $structure['dirname'];
		// create the dir, recursively
		if (!file_exists($structure) && !mkdir($structure, 0777, true)) {
    		die('Failed to create thumbnail folders: '.$structure);
		}
		
		// create a thumbnail..
		makeThumb($imagePath, $thumbPath, THUMB_LONG_EDGE);
	}
	
	return $thumbPath;
}

function getAlbumTitle($albumPath)
{
	$info = pathinfo($albumPath);
	$title = $info['basename'];
	$title = str_replace('_', ' ', $title);
	// make the first letter of every word uppercase
	return ucwords($title);
}

function getImageTitle($imagePath)
{
	$size = getimagesize($imagePath, $info);
	if(isset($info['APP13']))
	{
		$iptc = iptcparse($info['APP13']);
	    
		return $iptc["2#005"][0];
	}
	
	return '';
}

function albumList($d){ 
	$l = array();
	foreach (array_diff(scandir($d),array('.','..')) as $f)
	{
		// ignore the thumbs directory
		if ($f[0] == '_')
		{
			continue;
		}
		
		if (is_dir($d . '/' . $f))
		{
			$l[] = ($d == '.') ? $f : $d.'/'.$f;
		}
	}
	
	return $l; 
}

function getImageMenu($imagePath)
{
	$menu = '<ul class="menu"">';

	// get a list of all images in the album
	$pathInfo = pathinfo($imagePath);
	$images = imageList($pathInfo['dirname']);

	$index = 0;
	foreach ($images as $image)
	{
		if ($image == $imagePath)
		{
			if ($index > 0)
			{
				$menu .= '<li class="menu"><a href="index2.php?imagePath='.$images[$index - 1].'">Previous</a></li>';
			}
			if ($index < (count($images) - 1))
			{
				$menu .= '<li class="menu"><a href="index2.php?imagePath='.$images[$index + 1].'">Next</a></li>';
			}
			break;
		}
		$index++;
	}
	
	$menu .= '</ul>';
	
	return $menu;
}

// Breadcrumbs

function getAlbumBreadcrumb($albumPath)
{
	// breadcrumb...
	$crumb = '<p class="breadcrumbs"><a href="index2.php">Gallery</a>'."\n";
	
	if ($albumPath != '.')
	{
		$currentPath = '';
		foreach (explode('/', $albumPath) as $album)
		{
			// keep track of the url for this album
			$currentPath .= ($currentPath == '') ? $album : '/'.$album;
			$crumb .= ' / <a href="index2.php?albumPath='.$currentPath.'">'.getAlbumTitle($currentPath).'</a>'."\n";
		}
	}
	
	$crumb .= '</p>';
	
	return $crumb;
}

function getImageBreadcrumb($imagePath)
{
	$pathInfo = pathinfo($imagePath);
	$crumb = getAlbumBreadcrumb($pathInfo['dirname']);
	
	return $crumb;
}

// get a list of images in an album (directory)
function imageList($d){
	$l = array();
	foreach (array_diff(scandir($d),array('.','..')) as $f)
	{		
		if (!is_dir($d.'/'.$f))
		{
			$pathInfo = pathinfo($d.'/'.$f);
			if (in_array($pathInfo['extension'], array('jpg', 'JPG', 'jpeg', 'JPEG')))
			{
				$l[] = ($d == '.') ? $f : $d.'/'.$f;
			}
		}
	}
	
	return $l; 
}

function makeThumb($src, $dest, $desiredLongEdge)
{
	// read the source image
	$sourceImage = imagecreatefromjpeg($src);
	$width = imagesx($sourceImage);
	$height = imagesy($sourceImage);

	// find the "desired height" of this thumbnail, relative to the desired width
	if ($width > $height)
	{
		$desiredHeight = floor($height*($desiredLongEdge/$width));
		$desiredWidth = $desiredLongEdge;
	}
	else
	{
		$desiredHeight = $desiredLongEdge;
		$desiredWidth = floor($width*($desiredLongEdge/$height));
	}

	// create a new, "virtual" image
	$virtualImage = imagecreatetruecolor($desiredWidth,$desiredHeight);

	// copy source image at a resized size
	//imagecopyresized($virtualImage,$sourceImage,0,0,0,0,$desiredWidth,$desiredHeight,$width,$height);
	imagecopyresampled($virtualImage,$sourceImage,0,0,0,0,$desiredWidth,$desiredHeight,$width,$height);

	// create the physical thumbnail image to its destination
	imagejpeg($virtualImage, $dest, IMAGE_JPEG_QUALITY);
}


?>